<?php 
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{

	function postmail($to,$subject = '',$body = ''){
	    //Author:Jiucool WebSite: http://www.jiucool.com
	    //$to 表示收件人地址 $subject 表示邮件标题 $body表示邮件正文
	    //error_reporting(E_ALL);
	    //error_reporting(E_STRICT);
	    //date_default_timezone_set('Asia/Shanghai');//设定时区东八区
	    require_once('class.phpmailer.php');
	    include('class.smtp.php');
	    $mail             = new \PHPMailer(); //new一个PHPMailer对象出来
	    $body            = eregi_replace("[\]",'',$body); //对邮件内容进行必要的过滤
	    $mail->CharSet ="UTF-8";//设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
	    $mail->IsSMTP(); // 设定使用SMTP服务
	    $mail->SMTPDebug  = 1;                     // 启用SMTP调试功能
	    $mail->IsHTML(true);
	    // 1 = errors and messages
	    // 2 = messages only
	    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
	    //$mail->SMTPSecure = "ssl";                 // 安全协议，可以注释掉
	    $mail->Host       = 'smtp.sina.cn';      // SMTP 服务器
	    $mail->Port       = 25;                   // SMTP服务器的端口号
	    $mail->Username   = 'mizhi_wang';  // SMTP服务器用户名，PS：我乱打的
	    $mail->Password   = 'mizhiwang';            // SMTP服务器密码
	    $mail->From = 'mizhi_wang@sina.cn';
	    $mail->FromName = 'mizhi_wang';
	    $mail->AddReplyTo('mizhi_wang@sina.cn','mizhi_wang');
	    $mail->Subject    = $subject;
	    //$mail->AltBody    = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
	    $mail->MsgHTML($body);
	    $mail->AddAddress($to);
	    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
	    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
	    if(!$mail->Send()) {
	        echo 'Mailer Error: ' . $mail->ErrorInfo;
	    } else {
			echo "Message sent!恭喜，邮件发送成功！";
	    }
	    //$mail->Send();
	}
	
    public function vcode(){
        $scode = new \Think\Verify();
        $scode ->imageW = 100; //设置宽
        $scode ->imageH = 46; //设置宽
        $scode ->fontSize = 16; //设置字体大小
        $scode ->length = 4;
        $scode ->entry();
    }

	public function reg(){
		$user = D('User');
		//print_r(I('post.func'));
		
		if(IS_POST){
			$scode = new \Think\Verify();
			
			if($scode->check(I('post.vcode'))){
				if(I('post.func')=='找工作'){
					if($user->create()){
						$user->uname = trim(I('name'));
						$user->email = I('email');
						$user->password = md5(md5(I('password')));
						$user->regtime = time();	
						if($user->add()){
							$this->success('注册成功',U('home/index/index'),3);
							$this->postmail(I('email'),"您在[觅职网]上的账号已创建，请激活","你好: ".I('name').",欢迎加入觅职网<br>你需要点击以下链接来激活你的觅职账户:<br><a href='http://www.mizhi.com/confirm.php?user=0&name=".I('name')."&confirm_token=".md5(I('name')."@mizhi.com")."' target='_blank'>www.mizhi.com</a>");
							cookie('username',md5(I('post.name')));
							cookie('cname',I('post.name'));
						}else{
							$this->error('注册失败',U('home/user/reg'),3);
						}
					}else{
						echo $user->getError();
					}	
				}else if(I('post.func')=='招人'){
					$com = D('com');
					if($com->create()){
						$com->cname = I('name');
						$com->email = I('email');
						$com->password = md5(md5(I('password')));
						$com->regtime = time();
						if($com->add()){
							$this->success('注册成功',U('home/index/index'),3);
							$this->postmail(I('email'),"您在[觅职网]上的账号已创建，请激活","你好: ".I('name').",欢迎加入觅职网<br>你需要点击以下链接来激活你的觅职账户:<br><a href='http://www.mizhi.com/confirm.php?user=0&name=".I('name')."&confirm_token=".md5(I('name')."@mizhi.com")."' target='_blank'>www.mizhi.com</a>");
							cookie('username',md5(I('post.name')));
							cookie('cname',I('post.name'));
						}else{
							$this->error('注册失败',U('home/user/reg'),3);
						}
					}else{
						echo $com->getError();
					}
				}
			}else{
				echo "验证码错误";
				redirect(U('home/user/reg'));
			}	
		}else{
			$this->display('register');
		}
	}
	public function login(){
		
		if(IS_POST){
			$scode = new \Think\Verify();
			if($scode->check(I('vcode'))){
				if(I('post.uname')=='uname'){
					$user = D('Admin/User');
					$email = addslashes(trim(I('post.email')));
					$count = count($user->where('email="'.$email.'"')->select());
					//var_dump($user->where('email='.I("email"))->select());
					if($count){
						$users = $user->where('email="'.$email.'"')->find();
						//print_r($users[0]['password']);exit;
						if(md5(md5(I('post.password')))==$users['password']){
							cookie('username',md5($users['uname']));
							cookie('cname',$users['uname']);	
							$this->success('登录成功',U('/'),3);								
						}else{
							$this->error('密码错误');
						}	
						
					}else{
						$this->error("邮箱错误");
					}
				}else if(I('post.com')=='com'){
					$com = D('Admin/Com');
					if($com->create()){
						$email = trim(I('post.email'));
						$count = count($com->where('email="'.$email.'"')->select());
						//echo $count;
						if($count){
							$coms =$com->where('email="'.$email.'"')->find();
							//print_r($coms);exit;
							if(I('post.password')==$coms['password']){
								cookie('cname',$coms['cname']);	
								cookie('username',md5($coms['cname']));
								$this->success('登录成功',U('/'),3);
							}
						}else{
							$this->error('邮箱错误');
						}
					}else{
						$this->error($com->getError());
					}
				}
			}else{
				$this->error('验证码错误');
			}
		}else{
			$this->display();
		}
		
	}
}


?>